(function(){
    var c_city=Utils.getCookie("currentCity");
    var l_city=Utils.getCurrentCity("currentCity");
    var curr_city=document.getElementById("currentCity");
    console.log("index");
    function getCurr_city(){
        var  reqUrl='http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js';
        function callback(){
            var city=remote_ip_info.city;
            Utils.setCookie("currentCity",city);
            curr_city.innerHTML=city;
            setIndexData(curr_city.innerHTML);
        }
        //如果已经定位过,直接设置为已定位的，否则重新自动定位
        if(c_city||l_city){
            var city=c_city||l_city;
            Utils.setCookie("currentCity",city);
            curr_city.innerHTML=city;
            setIndexData(city);
        }else{
            $.getScript(reqUrl,callback);
        }
        $(".changecity a").on("click",function(){
            location.href="./address.html?currentCity="+curr_city.innerHTML;
        });
    }
    //定位城市
    getCurr_city(setIndexData);
    //根据当前城市curr_city来获取数据
    function setIndexData(current_city) {
        var address;

        if(c_city||l_city){
            address=c_city||l_city;
        }else{
            address=current_city;
        }
        console.log(address+"--"+current_city);
        //查看更多,跳转不同的页面
        $(".cont_more").on("click",function(){
            var link_more=$(this).attr("data-more");
            if(link_more=="product"){
                location.href="./goods.html?currentCity="+address;
            }else if(link_more=="shop"){
                location.href="./shop.html?currentCity="+address;
            }else if(link_more=="service"){
                location.href="./service.html?currentCity="+address;
            }else{
                console.log("系统错误");
            }
        });
        //页面跳转
        function jump_page(ele){
            var ele=ele;
            $(ele).on("click",function(){
                var links=$(this).attr("data-links");
                location.href="./"+links+".html?currentCity="+address;
            });   
        }
        jump_page(".jump_links");
        var currentPage=1;//默认设置当前页为第一页
        var totalPage=1;//设置默认总页数
        var show_count=12;//设置默认每次加载的条数
        var resType='1';//默认类型
        var phoneNO='';
        var areaId='';
        var shopName='';
        var url=Utils.reqUrl,//请求url
        async=true,
        type="post";
        //获取商家数据的接口
        var getShopList=JSON.stringify({
            "code":"getShopList",
            "verson":"1.0",
            "data":{
                "salesManId":"",
                "address":address,
                "sellerType":"",
                "resType":resType,
                "currentPage":currentPage,
                "showCount":show_count
            }
        });
        var getShopList=JSON.stringify({
            "code":"getShopList",
            "verson":"1.0",
            "data":{
                "phoneNO":phoneNO,
                "areaId":areaId,
                "salesManId":"",
                "address":address,
                "sellerType":"",
                "currentPage":currentPage,
                "showCount":show_count,
                "shopName":shopName,
            }
        });
        //获取商品数据的接口
        var getProductList=JSON.stringify({
            "code":"getProductList",
            "version":"1.0",
            "data":{
                "areaId":"",
                "parentPdTypeId":0,
                "userId":0,
                "address":address,
                "currentPage":currentPage,
                "showCount":show_count,
                "productName":"",
                "productTypeId":0
            }
        });
        //获取服务数据的接口
        var getServiceList=JSON.stringify({
            "code":"getLocalService",
            "version":"1.0",
            "data":{
                "address":address,
                "productType":"SERVICE",
                "serviceId":"",
                "productName":"",
                "resType":resType,
                "currentPage":currentPage,
                "showCount":show_count
            }
        });
        //ajax获取数据
        function getData(url,datas,call_func,async,type){
            console.log("请求参数:"+datas);
            $.ajax({
                url:url,
                type:type,
                datatype:"json",
                async:async,
                contentType:"application/json",
                data:datas,
                success:function(data){
                    console.log(data);
                    call_func(data);
                },
                error:function(msg){
                    console.log(msg);
                }         
            });        
        }
        //获取服务大类接口
        var serviceType=JSON.stringify({
             "code":"getPdOrServTypeList",
             "version":"1.0",
             "data":{
                 "parentServiceId":0,//服务大类id
                 "type":"SERVICE",
                 "parentPdTypeId":0//商家，商品大类id
             }
         });
        //获取商家，商品大类接口
        var shop_proType=JSON.stringify({
            "code":"getPdOrServTypeList",
            "version":"1.0",
            "data":{
                "parentServiceId":0,//服务大类id
                "type":"PRODUCT",
                "parentPdTypeId":0//商家，商品大类id
            }
        }); 
        //获取商家数据的回调函数
        function call_func_shop(data){
            var s_datas=data.data.datas;
            var s_len=s_datas.length;
            var shopStr='';
            for(var i=0;i<s_len;i++){
                var tagStr='';
                var tag_strs='';
                if(s_datas[i].sellerFees){
                    if(s_datas[i].sellerFees.length>0){
                        var SellerFree=s_datas[i].sellerFees[0].isAvaliable;
                        if(SellerFree=="1"){//已开通增值服务
                            var s_tag=s_datas[i].tagServices;
                            var ss_len=s_tag.length;

                            for(var s=0;s<ss_len;s++){
                                if(s_tag[s].sort=="1"){
                                    tag_strs+='<span class="bgfa6">'+s_tag[s].name+'</span>';    
                                }else if(s_tag[s].sort=="2"){
                                    tag_strs+='<span class="bg3bc">'+s_tag[s].name+'</span>';
                                }else if(s_tag[s].sort=="3"){
                                    tag_strs+='<span class="bgfaa">'+s_tag[s].name+'</span>';
                                }else{
                                    tagStr+='<img src="'+s_tag[s].tagIconPath+'">';
                                } 
                            }   

                        }       
                    }    
                }
                shopStr+='<li data-id="'+s_datas[i].shopId+'" class="marg_l10">'
                            +'<div class="data_lable_div">'+tag_strs+'</div>'
                            +'<img class="data_img lazy" title="'+s_datas[i].shopName+'" src="./images/loading.gif" data-original="'+s_datas[i].logoImgPath+'">'
                            +'<div class="pro_mark">'+s_datas[i].shopName+'</div>'
                            +'<div class="pro_detail">'
                                +'<p>'+s_datas[i].remark+'</p>'
                            +'</div>'
                            +'<div class="pro_lable">'
                               +'<img class="pro_rz_img" src="./images/renzheng.png" ><i class="pro_detail_name">'+s_datas[i].shopTypeName+'</i>'
                                +'<lable>'+tagStr+'</lable>'
                            +'</div>'
                        +'</li>';    
            }
            $(".shopList").html(shopStr);
            //点击当前商家，跳转进入商家详情页
            $(".shopList li").on("click",function(){
                var shopId=$(this).attr("data-id");
                location.href="./shopDetail.html?currentCity="+address+"&shopId="+shopId;
            });
            $("img.lazy").lazyload({
                threshold : 200
            });
        }
        //获取商品数据的回调函数
        function call_func_product(data){
            var p_datas=data.data.datas;//商品数据列
            var p_len=p_datas.length;//商品数据长度
            var p_str='';
            var arr1=[];//未开通增值服务
            var arr2=[];//已开通增值服务
            var dataStr1='';
            var dataStr2='';
            if(p_len>0){
                for(var i=0;i<p_len;i++){
                    if(p_datas[i].sellerFees.length>0){
                            arr2.push(p_datas[i]);    
                    }else{
                        arr1.push(p_datas[i]);
                    }
                }
                if(arr1.length>0){
                    for(var i=0,len=arr1.length;i<len;i++){
                       dataStr1+='<li data-id="'+arr1[i].productId+'" class="marg_l10">'
                                +'<div class="data_lable_div"></div>'
                                +'<img class="data_img lazy" title="'+arr1[i].productName+'" src="./images/loading.gif" data-original="'+arr1[i].mainImgPath+'" >'
                                +'<div class="pro_det"><img src="./images/renzheng.png" ><span class="pro_detail_name">'+arr1[i].productTypeName+'</span><span class="pro_det_price">￥'+arr1[i].saleprice+'</span></div>'
                                +'<div class="pro_mark">'+arr1[i].productName+'</div>'
                                +'<div class="pro_detail">'
                                    +'<p>'+arr1[i].remark+'</p>'
                                +'</div>'
                                +'<div class="pro_lable">'
                                +'</div>'
                            +'</li>';
                    }
                }
                var downS='';
                if(arr2.length>0){
                    for(var i=0,len=arr2.length;i<len;i++){
                        var tagStr='';
                        if(arr2[i].sellerFees){
                            if(arr2[i].sellerFees.length>0){
                                var SellerFree=arr2[i].sellerFees[0].isAvaliable;
                                if(SellerFree=="1"){//已开通增值服务
                                    var s_tags=arr2[i].tagServices;
                                    var ss_len=s_tags.length;
                                    var tag_strs='';
                                    for(var s=0;s<ss_len;s++){
                                        if(s_tags[s].sort=="1"){
                                            tag_strs+='<span class="bgfa6">'+s_tags[s].name+'</span>';    
                                        }else if(s_tags[s].sort=="2"){
                                            tag_strs+='<span class="bg3bc">'+s_tags[s].name+'</span>';
                                        }else if(s_tags[s].sort=="3"){
                                            tag_strs+='<span class="bgfaa">'+s_tags[s].name+'</span>';
                                        }else{
                                            tagStr+='<img src="'+s_tag[s].tagIconPath+'">';
                                        } 
                                    }

                                }       
                            }    
                        }
                        dataStr2+='<li data-id="'+arr2[i].productId+'" class="marg_l10">'
                                    +'<div class="data_lable_div">'+tag_strs+'</div>'
                                    +'<img class="data_img lazy" title="'+arr2[i].productName+'" src="./images/loading.gif" data-original="'+arr2[i].mainImgPath+'" >'
                                    +'<div class="pro_det"><img src="./images/renzheng.png" ><span class="pro_detail_name">'+arr2[i].productTypeName+'</span><span class="pro_det_price">￥'+arr2[i].saleprice+'</span></div>'
                                    +'<div class="pro_mark">'+arr2[i].productName+'</div>'
                                    +'<div class="pro_detail">'
                                        +'<p>'+arr2[i].remark+'</p>'
                                    +'</div>'
                                    +'<div class="pro_lable">'
                                        +tagStr
                                    +'</div>'
                                +'</li>';
                    }
                }
                $(".productList").html(dataStr2+dataStr1);
                //点击当前商品，跳转进入商家详情页
                $(".productList li").on("click",function(){
                    var productId=$(this).attr("data-id");
                    location.href="./goodDetail.html?currentCity="+address+"&productId="+productId;
                });
            }
            $("img.lazy").lazyload({
                threshold : 200
            });
        }
        //获取服务数据的回调函数
        function call_func_service(data){
            var serv_datas=data.data.datas;//数据列
            var serv_len=serv_datas.length;//数据列个数
            var servStrs='';
            for(var i=0;i<serv_len;i++){
                servStrs+='<li data-id="'+serv_datas[i].productId+'" class="marg_l10 ser_li">'
                                +'<div class="data_lable_div"></div>'
                                +'<div class="shadow_bg"></div>'
                                +'<div class="shadow_del"><span class="sha_type">'+serv_datas[i].serviceName+'</span><img class="sha_img" src="./images/collect.png" ><span class="sha_cout">'+serv_datas[i].serviceCollectCount+'</span><img class="sha_img" src="./images/view.png" ><span class="sha_cout">'+serv_datas[i].viewCount+'</span></div>'
                                +'<img class="data_img lazy" title="'+serv_datas[i].productName+'"  data-original="'+serv_datas[i].userImgPath+'" src="./images/loading.gif">'
                                +'<div class="pro_mark">'+serv_datas[i].productName+'</div>'
                                +'<div class="pro_detail">'
                                    +'<p>'+serv_datas[i].remark+'</p>'
                                +'</div>'

                                +'<div class="pro_lable">'
                                   +'<img class="pro_rz_img" src="./images/renzheng.png" ><i class="pro_detail_name">'+serv_datas[i].serviceName+'</i>'
                                    +'<lable>'
                                        +'<span class="lab_price">￥'+serv_datas[i].saleprice+'</span>'
                                        +'/<span class="lab_unit">'+serv_datas[i].unit+'</span>'
                                    +'</lable>'
                                +'</div>'
                            +'</li>';
            }
            $(".serviceList").html(servStrs);
            //点击当前服务，跳转进入商家详情页
            $(".serviceList li").on("click",function(){
                var serviceId=$(this).attr("data-id");
                location.href="./serviceDetail.html?productId="+serviceId;
            });
            $("img.lazy").lazyload({
                threshold : 200
            });
        }
        //大类 服务id
        var p_serId;
        //获取服务类的回调函数
        function call_func_serType(data){
            var serType_datas=data.data.serviceTypes;
            var ser_len=serType_datas.length;
            var typeStr='';
            for(var i=0;i<ser_len;i++){
                typeStr+='<li data-id="'+serType_datas[i].serviceId+'" class="service_nav_li l">'+serType_datas[i].serviceName+'<span></span></li>'; 
            }
            $(".service_nav").html(typeStr);
            setAction(".service_nav",".service_nav_kid");//服务
            $(".service_nav").find("li").on("mouseover",function(){
                 p_serId=parseInt($(this).attr("data-id"));
                 var kid_s_Type=JSON.stringify({
                     "code":"getPdOrServTypeList",
                     "version":"1.0",
                     "data":{
                         "parentServiceId":p_serId,
                         "type":"SERVICE",
                         "parentPdTypeId":0
                     }
                 });
                getData(url,kid_s_Type,call_func_s_kid,async,type); 
            });
            function call_func_s_kid(data){
                var kid_datas=data.data.serviceTypes;
                var kid_len=kid_datas.length;
                var kidStr='';
                for(var j=0;j<kid_len;j++){
                    kidStr+='<li data-serId="'+kid_datas[j].serviceId+'" class="nav_kid_li l">'+kid_datas[j].serviceName+'</li>';
                }
                $(".service_nav_kid .nav_kid").html(kidStr);
                //服务跳转
                $(".service_nav_kid li").on("click",function(){
                    var ser_Id=$(this).attr("data-serId");
                    location.href="./service.html?currentCity="+address+"&serviceId="+ser_Id+"&parentServiceId="+p_serId;//跳转到服务页面
                });
            }
        }
        //类型菜单选择样式
        function setAction(ele,_ele){
            var ele=ele;
            var _ele=_ele;
            $(ele).on({//大类鼠标停留跟移除
                mouseover:function(){
                    show_action();
                },
                mouseout:function(){
                    hide_action();    
                }
            });
            $(_ele).find(".nav_kid").on({//小类鼠标停留跟移除
                mouseover:function(){
                    show_action();
                },
                mouseout:function(){
                    hide_action();
                }
            });
            function show_action(){
                $(_ele).addClass("show").removeClass("hide");
            }
            function hide_action(){
                $(_ele).addClass("hide").removeClass("show");
            }
            $(document).on("click",_ele+" ul li",function(){//选择小类并且隐藏，跳转到指定的页面
                $(_ele).addClass("hide").removeClass("show");
            }); 
        }
        //获取商家类的回调函数
        function call_func_shopType(data){
            var shop_datas=data.data.productTypes;
            var shop_len=shop_datas.length;
            var typeStr='';
            for(var i=0;i<shop_len;i++){
                typeStr+='<li data-id="'+shop_datas[i].pdTypeId+'" class="shop_nav_li l">'+shop_datas[i].pdTypeName+'<span></span></li>';         
            }
            $(".shop_nav").html(typeStr);
            $(".product_nav").html(typeStr);
            setAction(".shop_nav",".shop_nav_kid");//商家
            setAction(".product_nav",".product_nav_kid");//商品

            $(".shop_nav").find("li").on("mouseover",function(){
                 var proId=parseInt($(this).attr("data-id"));
                 var kid_p_Type=JSON.stringify({
                     "code":"getPdOrServTypeList",
                     "version":"1.0",
                     "data":{
                         "parentServiceId":0,
                         "type":"PRODUCT",
                         "parentPdTypeId":proId
                     }
                 });
                getData(url,kid_p_Type,call_func_p_kid,async,type);
            });

            function call_func_p_kid(data){//商家子类
                var kid_datas=data.data.productTypes;
                var kid_len=kid_datas.length;
                var kidStr='';
                for(var j=0;j<kid_len;j++){
                    kidStr+='<li data-p-id="'+kid_datas[j].parentPdTypeId+'"  data-id="'+kid_datas[j].pdTypeId+'"  class="nav_kid_li l">'+kid_datas[j].pdTypeName+'</li>';
                }
                $(".shop_nav_kid .nav_kid").html(kidStr);
                 //通过选择商家类型，跳入商家页
                $(".shop_nav_kid ul li").off("click");
                $(".shop_nav_kid ul li").on("click",function(){
                    var p_id=$(this).attr("data-id");
                    var parent_type_id=$(this).attr("data-p-id");
                    location.href="./shop.html?currentCity="+address+"&parent_type_id="+parent_type_id+"&p_type_id="+p_id;
                });
            }
            $(".product_nav").find("li").on("mouseover",function(){
                 var proId=parseInt($(this).attr("data-id"));
                 var kid_p_Type=JSON.stringify({
                     "code":"getPdOrServTypeList",
                     "version":"1.0",
                     "data":{
                         "parentServiceId":0,
                         "type":"PRODUCT",
                         "parentPdTypeId":proId
                     }
                 });
                getData(url,kid_p_Type,call_func_sp_kid,async,type);
            });
            function call_func_sp_kid(data){//商品子类
                var kid_datas=data.data.productTypes;
                var kid_len=kid_datas.length;
                var kidStr='';
                for(var j=0;j<kid_len;j++){
                    kidStr+='<li data-p-id="'+kid_datas[j].parentPdTypeId+'"  data-id="'+kid_datas[j].pdTypeId+'" class="nav_kid_li l">'+kid_datas[j].pdTypeName+'</li>';
                }
                $(".product_nav_kid .nav_kid").html(kidStr);
                //通过选择商品类型，跳入商品页
                $(".product_nav_kid ul li").off("click");
                $(".product_nav_kid ul li").on("click",function(){
                    var p_id=$(this).attr("data-id");
                    var parent_type_id=$(this).attr("data-p-id");
                    location.href="./goods.html?currentCity="+address+"&parent_type_id="+parent_type_id+"&p_type_id="+p_id;
                });
            }
        }
        //获取商家数据
        getData(url,getShopList,call_func_shop,async,type);
        //获取商品
        getData(url,getProductList,call_func_product,async,type);
        //获取服务
        getData(url,getServiceList,call_func_service,async,type);
        //获取商家大类
        getData(url,shop_proType,call_func_shopType,async,type);
        //获取服务大类
        getData(url,serviceType,call_func_serType,async,type);
        //请求参数可封装成一个方法
    };
})();